package book;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class tcpClient_book {

    private Socket socket;

    public tcpClient_book(String ip, int port) {
        try {
            //建立和服务器之间的连接
            socket = new Socket(ip, port);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void start() {
        try (InputStream in = socket.getInputStream();
             OutputStream out = socket.getOutputStream()) {
            while (true) {

                // 这里我给in和out套层壳，可以使用我们经常用到的方法
                Scanner scanner = new Scanner(in);
                PrintWriter writer = new PrintWriter(out);

                System.out.println("连接成功");
                // 读取请求

                Scanner sc = new Scanner(System.in);
                String request = sc.nextLine();

                //将请求发送出去
                writer.println(request);
                writer.flush();

                //读取响应
                System.out.println(scanner.nextLine());
            }

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        tcpClient_book book = new tcpClient_book("127.0.0.1", 9990);
        book.start();
    }
}
